package staff

import (
	"duoke/model"
	"duoke/service"
	"duoke/tables"
	"duoke/utils/orm"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var Order = new(orderCtl)

type orderCtl struct{}

func (ctl *orderCtl) List(c *gin.Context) {
	staffR, _ := service.Request.Staff(c)
	var req model.ReqList
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	list := make([]tables.Order, 0)
	query := orm.Db.Where("goods_id =? AND staff_id =?", staffR.GoodsId, staffR.Id).Desc("id")
	if req.Keyword != "" {
		query = query.Where("(nickname =? OR phone =? OR name =?)", req.Keyword, req.Keyword, req.Keyword)
	}
	if req.PayState != "" {
		query = query.Where("pay_state =?", req.PayState)
	}
	err := query.Find(&list)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, req.PayState, list)
}
